﻿
using BLL;
using System;
using System.Web;
using WeChat.Common;
using WeChat.WxPayAPI.lib;
using System.Configuration;
using Newtonsoft.Json;

namespace WxPayAPI
{
    public class H5Pay
    {
        public string GetPayUrl(string thip, string Opid, string Money)
        {
#pragma warning disable CS0618 // “ConfigurationSettings.AppSettings”已过时:“This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings”
            string srcUrl = ConfigurationSettings.AppSettings["srcUrl"];
#pragma warning restore CS0618 // “ConfigurationSettings.AppSettings”已过时:“This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings”
            Log.Info(this.GetType().ToString(), "H5 pay url is producing...");
            //生成随机数
            string OrderNum = WxPayApi.GenerateOutTradeNo();
            string NumID = ReBLL.NumID().ToString();
            DateTime DateStat = DateTime.Now;
            WxPayData data = new WxPayData();
            data.SetValue("body", "校园卡存款");//商品描述
            data.SetValue("attach", "校园卡存款");//附加数据
            data.SetValue("out_trade_no", OrderNum);//商户订单号
            data.SetValue("total_fee", Money);//总金额
            data.SetValue("time_start", DateStat.ToString("yyyyMMddHHmmss"));//交易起始时间
            data.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss"));//交易结束时间
            data.SetValue("goods_tag", "f");//商品标记
            data.SetValue("spbill_create_ip", thip);//终端IP
            data.SetValue("trade_type", "MWEB");//交易类型
            data.SetValue("product_id", NumID);//商品ID
            data.SetValue("notify_url", srcUrl + "JsSDK/WxPay/ResultNotifyPage.aspx");
            data.SetValue("scene_info", "{'h5_info':{'type':'Wap','wap_url':'wechat.jocat.com','wap_name':'你的商城名称'}}");//场景信息



            ApplicationLog.Write($"H5下单参数={JsonConvert.SerializeObject(data.GetValues())}");
            WxPayData result = H5.UnifiedOrder(data);//调用统一下单接口
            string url = result.GetValue("mweb_url").ToString();//获得统一下单接口返回的二维码链接
            ApplicationLog.Write("调用统一下单商户订单号：" + OrderNum + ",result=" + JsonConvert.SerializeObject(result));
            //获取卡号
            //string ryid = ReBLL.ObtainRfkh(Opid);
            //下单信息添加到OrderNumber
            //ReBLL.OrderNum(OrderNum, Convert.ToDouble(Money), ryid, DateStat,2);
            //Log.Info(this.GetType().ToString(), "Get H5 pay url : " + url);
            return url;
        }
    }
}
